/*
   tsAPI Distribution
   ts_NavBox Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/

function ts_NavBox( img, wid, hei, bgcolor, bordercolor ) {
	this.DynLayer = DynLayer;
        this.DynLayer(null,0,0,wid,hei);

	this.nv_w = wid;
	this.nv_h = hei;
	this.mx_w = wid;
	this.mx_h = hei;

	this.nv_iw = wid;
	this.nv_ih = hei;

	this.imgN = img;
	this.nvimg = new DynImage(this.imgN);

	var ui_frame = new DynLayer( null,0,0,wid,hei,bordercolor );
	var ui_canvas = new DynLayer( null,1,1,wid-2,hei-2,bgcolor );
	
	this.addChild( ui_frame );
	this.addChild( ui_canvas );
	this.addChild( this.nvimg );

	var lineColor="#FF0000";

	this.l1 = new DynLayer( null,-1,-1,this.nv_w,1,lineColor );
	this.l2 = new DynLayer( null,-1,-1,this.nv_w,1,lineColor );
	this.l3 = new DynLayer( null,-1,-1,1,this.nv_h,lineColor );
	this.l4 = new DynLayer( null,-1,-1,1,this.nv_h,lineColor );
	this.addChild( this.l1 );
	this.addChild( this.l2 );
	this.addChild( this.l3 );
	this.addChild( this.l4 );

};


ts_NavBox.prototype = new DynLayer;


ts_NavBox.prototype.setNavImage = function ( img,offx,offy,zw,zh ) {
	if( this.imgN != img ) {
		this.imgN = img;
		this.nvimg.setImage(this.imgN);
	};

	this.mx_w = zw+offx*2; 
	this.mx_h = zh+offy*2;

	var scale_factor = Math.min( (this.nv_w-2)/this.mx_w , (this.nv_h-2)/this.mx_h  );

	this.nv_iw = zw*scale_factor;
	this.nv_ih = zh*scale_factor;

	this.nvimg.setSize( this.nv_iw, this.nv_ih );
	this.nvimg.moveTo( 1+(offx+(this.mx_w<this.mx_h?(this.mx_h-this.mx_w)/2:0))*scale_factor , 1+(offy+(this.mx_h<this.mx_w?(this.mx_w-this.mx_h)/2:0))*scale_factor );

	this.l1.setZIndex(1); this.l2.setZIndex(1); this.l3.setZIndex(1); this.l4.setZIndex(1);
};

ts_NavBox.prototype.rgnshift = function (x,y,w,h) {		//x,y,w,h are sizes of the background layer
	var p1x = Math.max( 1, Math.min( this.nv_w-2, Math.round(this.nvimg.x-x/w*this.nv_iw) ));
	var p1y = Math.max( 1, Math.min( this.nv_h-2, Math.round(this.nvimg.y-y/h*this.nv_ih) ));
	var p2x = Math.max( 1, Math.min( this.nv_w-2, Math.round(this.nvimg.x-(x-this.mx_w+2)/w*this.nv_iw)-1 ));
	var p2y = Math.max( 1, Math.min( this.nv_h-2, Math.round(this.nvimg.y-(y-this.mx_h+2)/h*this.nv_ih)-1 ));

	if( p1x <= 1 && p1y <= 1 && p2x >= this.nv_w-2 && p2y >= this.nv_h-2 ) {
		this.l1.moveTo(-1,-1);
		this.l2.moveTo(-1,-1);
		this.l3.moveTo(-1,-1);
		this.l4.moveTo(-1,-1);
	} else {

		this.l1.moveTo(p1x,p1y);	this.l1.setSize(p2x-p1x+1,1);
		this.l2.moveTo(p1x,p2y);	this.l2.setSize(p2x-p1x+1,1);
		this.l3.moveTo(p1x,p1y);	this.l3.setSize(1,p2y-p1y+1);
		this.l4.moveTo(p2x,p1y);	this.l4.setSize(1,p2y-p1y+1);
	};
};


